/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;


import java.util.UUID;
import Aptech.Common.Formater;
import Aptech.Service.SV_Buitls;
import Aptech.Service.SV_Lands;
import Form.BuiltsForm;
import Form.LandsForm;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Lands;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author nkhoan
 */
public class BuiltsAction extends DispatchAction {
    
     private SV_Buitls mgBuilt = new SV_Buitls();
     private   SV_Lands mgLand = new SV_Lands();
     private  BuiltsForm builtForm;
     
    public ActionForward getAllBuilt(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
             builtForm = (BuiltsForm) form;
             builtForm.setArrBuilts(mgBuilt.getAllBuilt());
             request.setAttribute("sitemap", "List of Built");

        return mapping.findForward("list");
    }

        public ActionForward editBuilt(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        	
        	builtForm = (BuiltsForm) form;
        	String id = request.getParameter("id");
        	request.setAttribute("sitemap", "Edit A Built");
        	
        	String type = request.getParameter("type1");
        	if (type.equals("edit1")) {
                request.setAttribute("id", id);
                request.setAttribute("type1", type);
                request.setAttribute("arrLands", mgLand.getAllLand());
                // Set value for Form
                Builts built = mgBuilt.getBuiltByID(id);
                builtForm.setName(built.getName());
                builtForm.setAdress(built.getAdress());
                builtForm.setStartDate(Formater.date2str(built.getStartDate()));
                builtForm.setFinishDate(Formater.date2str(built.getFinishDate()));
                builtForm.setNote(built.getNote());
                builtForm.setType(built.getType());
                builtForm.setFloos(built.getFloos());
                builtForm.setIdLand(built.getIdLand());
                builtForm.setId(id);

               
            } else {
            	request.setAttribute("arrLands", mgLand.getAllLand());
            	Builts built = new Builts();
                built.setName(builtForm.getName());
                built.setAdress(builtForm.getAdress());
                built.setFloos(builtForm.getFloos());
                built.setId(builtForm.getId());              
                built.setStartDate(Formater.str2SQLdate(builtForm.getStartDate()));
                built.setFinishDate(Formater.str2SQLdate(builtForm.getFinishDate()));            
                built.setIdLand(builtForm.getIdLand());
                built.setType(builtForm.getType());
                built.setNote(builtForm.getNote());
                
                mgBuilt.editBuilt(built);
                request.setAttribute("save", "save");
            }


            return map.findForward("edit");
    }
        public ActionForward addBuilt(ActionMapping map, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        	
        	builtForm = (BuiltsForm) form;
        	request.setAttribute("arrLands", mgLand.getAllLand());
            String type = request.getParameter("type1");
            request.setAttribute("sitemap", "Add A New Built");
            if (type == null) {
                request.setAttribute("type1", "add");
                return map.findForward("edit");
            } else {
                UUID id = UUID.randomUUID();
                Builts built = new Builts();
                built.setName(builtForm.getName());
                built.setAdress(builtForm.getAdress());
                built.setFloos(builtForm.getFloos());
                built.setId(id.toString());              
                built.setStartDate(Formater.str2SQLdate(builtForm.getStartDate()));
                built.setFinishDate(Formater.str2SQLdate(builtForm.getFinishDate()));
                built.setNote(builtForm.getNote());
                built.setIdLand(builtForm.getIdLand());
                built.setType(builtForm.getType());
                
                mgBuilt.addBuilt(built);
                request.setAttribute("save", "save");
            }

            return map.findForward("edit");
        }
        
        public ActionForward deleteBuilt(ActionMapping map, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        	
        	String id = request.getParameter("id");
            mgBuilt.deleteBuilt(id);
            response.sendRedirect("/NTB/built.do?method=getAllBuilt");
            
        	return map.findForward("list");
        }
        
        public ActionForward detailBuilt(ActionMapping map, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        	
        	builtForm = (BuiltsForm) form;
            String id = request.getParameter("id");
            request.setAttribute("id", id);
            request.setAttribute("type1", "detail");
            request.setAttribute("arrLands", mgLand.getAllLand());
            request.setAttribute("sitemap", "Detail of A Built");
                // Set value for Formpe
                Builts built = mgBuilt.getBuiltByID(id);
                builtForm.setName(built.getName());           
                builtForm.setAdress(built.getAdress());
                builtForm.setStartDate(Formater.date2str(built.getStartDate()));
                builtForm.setFinishDate(Formater.date2str(built.getFinishDate()));
                builtForm.setFloos(built.getFloos());
                builtForm.setNote(built.getNote());
                builtForm.setType(built.getType());
                builtForm.setIdLand(built.getIdLand());
                
        	return map.findForward("edit");
        }
}
